<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
    title="个人信息"
    left-arrow
    @click-left="$router.back()"
    />
    <!-- /导航栏 -->
    <van-cell title="头像" is-link @click="$refs.file.click()">
      <van-image
      class="avatar"
      fit="cover"
      round
      :src="user.photo"
      />
    </van-cell>
    <input type="file" hidden ref="file" @change="inputChange"/>
    <van-cell title="昵称"
    :value="user.name"
    is-link
    @click="isnichengshow = true"
    />
    <van-cell title="性别"
    :value="user.gender ? '女' : '男'"
    is-link
    @click="isxinbieshow = true"
    />
    <van-cell
    title="生日"
    :value="user.birthday || '1977-00-00'"
    is-link
    @click="isshengrishow = true"
    />

    <!-- 编辑昵称弹层 -->
    <van-popup
    v-model="isnichengshow"
    style="height: 100%"
    position="bottom"
    >
      <updateName
      v-if="isnichengshow"
      @close="isnichengshow = false"
      v-model="user.name"
      />
    </van-popup>
    <!-- /编辑昵称弹层 -->

    <!-- 编辑性别弹层 -->
    <van-popup
    v-model="isxinbieshow"
    position="bottom"
    >
      <updateSex
      v-if="isxinbieshow"
      v-model="user.gender"
      @close="isxinbieshow = false"
      />
    </van-popup>
    <!-- /编辑性别弹层 -->

    <!-- 编辑生日弹层 -->
    <van-popup
    v-model="isshengrishow"
    position="bottom"
    >
      <updateshengri
      v-if="isshengrishow"
      v-model="user.birthday"
      @close="isshengrishow = false"
      />
    </van-popup>
    <!-- 、编辑生日弹层 -->

    <!-- 编辑头像弹层 -->
    <van-popup
    v-model="istouxiangshow"
    style="height: 100%"
    position="bottom"
    >
      <updatetouxiang
      v-if="istouxiangshow"
      :img="img"
      @colse="istouxiangshow = false"
      @updata-photo="user.photo = $event"
      />
    </van-popup>
    <!-- /编辑头像弹层 -->
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import updateName from './components/update-name.vue'
import updateSex from './components/update-sex.vue'
import updateshengri from './components/update-shengri.vue'
import updatetouxiang from './components/update-touxiang.vue'
export default {
  name: 'UserProfile',
  components: {
    updateName,
    updateSex,
    updateshengri,
    updatetouxiang
  },
  props: {
  },
  data () {
    return {
      user: {},
      img: {},
      isnichengshow: false,
      isxinbieshow: false,
      isshengrishow: false,
      istouxiangshow: false
    }
  },
  created () {
    this.loadProfile()
  },
  methods: {
    async loadProfile () {
      try {
        const { data } = await getUserProfile()
        this.user = data.data
      } catch (err) {
        this.$toast('操作失败,请重试')
      }
    },
    inputChange () {
      // 获取文件对象
      const file = this.$refs.file.files[0]
      // 获取blob 数据
      this.img = window.URL.createObjectURL(file)
      this.istouxiangshow = true
      this.$refs.file.value = ''
    }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-popup {
    background-color: #f5f7f9;
  }
}
</style>
